﻿using System.Collections.Generic;
using System.Linq;
using Brouteforce.OldStuff.oldGameObjs;

namespace Brouteforce.OldStuff.uiOld {
    public interface ISelectableGameObject : IGameObject {
        bool Selected { get; set; }
    }
    public class SingleSelectionGroup {
        private readonly List<ISelectableGameObject> _group = new List<ISelectableGameObject>();
        public ISelectableGameObject Selection = null;

        public void Add(ISelectableGameObject c){
            _group.Add(c);
            if (Selection == null){
                Selection = c;
                c.Selected = true;
            }else{
                c.Selected = false;
            }
        }

        public void Update(){
            foreach (var c in _group){
                bool selected = c.Selected;
                c.Update();
                if (!selected && c.Selected){
                    Selection = c;
                }
            }

            //fix selection
            foreach (var c in _group.Where(c => c != Selection)){
                c.Selected = false;
            }
        }

        public void Draw(int cameraX, int cameraY) {
            _group.ForEach(c => c.Draw(cameraX, cameraY));
        }
    }
}
